/*
 * @Author: Robin
 * @Date: 2020/11/6 17:13
 * @LastEditors: Robin
 * @LastEditTime: 2021-03-29 10:44:21
 * @Description: MSSTV 帮助工具
 */
package main

import (
	"io"
	"log"
	"os"
	"strconv"
	"strings"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

const PATH = "./msstv_res/"       // msstv路径
const FILE_PATH = "./msstv_file/" // 目标路径

var (
	db  *gorm.DB
	err error
)

func main() {
	// 连接sqlite数据库
	db, err = gorm.Open(sqlite.Open(PATH+"/mss.db"), &gorm.Config{})
	if err != nil {
		log.Fatalln(err)
	}
	Company()
	Market()
	Product()
	Technical()
	Manual()
}

func Company() {
	dir := "0@公司介绍@Company@none"
	filePath := FILE_PATH + dir + "/"
	category := make([]Category, 0)
	db.Where("pid = ?", 1).Order("sort desc").Find(&category)
	// 开始遍历
	for i, v := range category {
		dir2 := strconv.Itoa(i) + "@" + v.TitleCN + "@" + v.TitleEN + "@" + v.Type
		// 创建目录
		cnPath := filePath + dir2 + "/cn"
		enPath := filePath + dir2 + "/en"
		os.MkdirAll(cnPath, 0777)
		os.MkdirAll(enPath, 0777)
		// 封面
		ext := strings.Split(v.Icon, ".")
		_, err = CopyFile(filePath+dir2+"."+ext[1], PATH+v.Icon)
		// 搜集资源
		var fs FileSource
		err = db.Where("category_id = ?", v.ID).First(&fs).Error
		// 拆分归类资源
		ff := strings.Split(fs.CN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(cnPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
		ff = strings.Split(fs.EN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(enPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
	}
}

func Market() {
	dir := "1@市场领域@Markets@none"
	filePath := FILE_PATH + dir + "/"
	category := make([]Category, 0)
	db.Where("pid = ?", 3).Order("sort desc").Find(&category)
	// 开始遍历
	for i, v := range category {
		dir2 := strconv.Itoa(i) + "@" + v.TitleCN + "@" + v.TitleEN + "@" + v.Type
		// 创建目录
		cnPath := filePath + dir2
		enPath := filePath + dir2
		os.MkdirAll(cnPath, 0777)
		os.MkdirAll(enPath, 0777)
		// 封面
		ext := strings.Split(v.Icon, ".")
		_, err = CopyFile(filePath+dir2+"."+ext[1], PATH+v.Icon)
		// 查询子项
		c1 := make([]Category, 0)
		db.Where("pid = ?", v.ID).Order("sort desc").Find(&c1)
		for i1, v1 := range c1 {
			dir3 := strconv.Itoa(i1+100) + "@" + v1.TitleCN + "@" + v1.TitleEN + "@" + v1.Type
			// 创建目录
			cnPath := filePath + dir2 + "/" + dir3 + "/cn"
			enPath := filePath + dir2 + "/" + dir3 + "/en"
			os.MkdirAll(cnPath, 0777)
			if v1.Type != "app" {
				os.MkdirAll(enPath, 0777) // 非app，创建英文目录
			}
			// 封面
			ext := strings.Split(v1.Icon, ".")
			_, err = CopyFile(filePath+dir2+"/"+dir3+"."+ext[1], PATH+v1.Icon)
			// 搜集资源
			var fs FileSource
			err = db.Where("category_id = ?", v1.ID).First(&fs).Error
			// 拆分归类资源
			if v1.Type == "app" {
				ff := strings.Split(fs.CN, "#")
				for j1, f1 := range ff {
					ff2 := strings.Split(f1, ",")
					_, err = CopyFile(cnPath+"/"+strconv.Itoa(j1+100)+"@"+ff2[0], PATH+ff2[0])
					if err != nil {
						log.Fatalln(err)
					}
					_, err = CopyFile(cnPath+"/"+strconv.Itoa(j1+100)+"@"+ff2[1], PATH+ff2[1])
					if err != nil {
						log.Fatalln(err)
					}
				}
				// 英文暂时使用中文
				// ff = strings.Split(fs.EN, "#")
				// for j1, f1 := range ff {
				// 	ff2 := strings.Split(f1, ",")
				// 	_, err = CopyFile(enPath+"/"+strconv.Itoa(j1+100)+"@"+ff2[0], PATH+ff2[0])
				// 	if err != nil {
				// 		log.Fatalln(err)
				// 	}
				// 	_, err = CopyFile(enPath+"/"+strconv.Itoa(j1+100)+"@"+ff2[1], PATH+ff2[1])
				// 	if err != nil {
				// 		log.Fatalln(err)
				// 	}
				// }
			} else {
				ff := strings.Split(fs.CN, "#")
				for j1, f1 := range ff {
					_, err = CopyFile(cnPath+"/"+strconv.Itoa(j1+100)+"@"+f1, PATH+f1)
					if err != nil {
						log.Fatalln(err)
					}
				}
				ff = strings.Split(fs.EN, "#")
				for j1, f1 := range ff {
					_, err = CopyFile(enPath+"/"+strconv.Itoa(j1+100)+"@"+f1, PATH+f1)
					if err != nil {
						log.Fatalln(err)
					}
				}
			}
		}
	}
}

func Product() {
	dir := "2@产品分类@Products@none"
	filePath := FILE_PATH + dir + "/"
	category := make([]Category, 0)
	db.Where("pid = ?", 2).Order("sort desc").Find(&category)
	// 开始遍历
	for i, v := range category {
		dir2 := strconv.Itoa(i+100) + "@" + v.TitleCN + "@" + v.TitleEN + "@" + v.Type
		// 创建目录
		cnPath := filePath + dir2 + "/cn"
		enPath := filePath + dir2 + "/en"
		os.MkdirAll(cnPath, 0777)
		os.MkdirAll(enPath, 0777)
		// 封面
		ext := strings.Split(v.Icon, ".")
		_, err = CopyFile(filePath+dir2+"."+ext[1], PATH+v.Icon)
		// 搜集资源
		var fs FileSource
		err = db.Where("category_id = ?", v.ID).First(&fs).Error
		// 拆分归类资源
		ff := strings.Split(fs.CN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(cnPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
		ff = strings.Split(fs.EN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(enPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
	}
}

func Technical() {
	dir := "3@技术资料@Technical@none"
	filePath := FILE_PATH + dir + "/"
	category := make([]Category, 0)
	db.Where("pid = ?", 4).Order("sort desc").Find(&category)
	// 开始遍历
	for i, v := range category {
		dir2 := strconv.Itoa(i+100) + "@" + v.TitleCN + "@" + v.TitleEN + "@" + v.Type
		// 创建目录
		cnPath := filePath + dir2 + "/cn"
		enPath := filePath + dir2 + "/en"
		os.MkdirAll(cnPath, 0777)
		os.MkdirAll(enPath, 0777)
		// 封面
		ext := strings.Split(v.Icon, ".")
		_, err = CopyFile(filePath+dir2+"."+ext[1], PATH+v.Icon)
		// 搜集资源
		var fs FileSource
		err = db.Where("category_id = ?", v.ID).First(&fs).Error
		// 拆分归类资源
		ff := strings.Split(fs.CN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(cnPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
		ff = strings.Split(fs.EN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(enPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
	}
}

func Manual() {
	dir := "4@使用手册@Manuals@none"
	filePath := FILE_PATH + dir + "/"
	category := make([]Category, 0)
	db.Where("pid = ?", 5).Order("sort desc").Find(&category)
	// 开始遍历
	for i, v := range category {
		dir2 := strconv.Itoa(i+100) + "@" + v.TitleCN + "@" + v.TitleEN + "@" + v.Type
		// 创建目录
		cnPath := filePath + dir2 + "/cn"
		enPath := filePath + dir2 + "/en"
		os.MkdirAll(cnPath, 0777)
		os.MkdirAll(enPath, 0777)
		// 封面
		ext := strings.Split(v.Icon, ".")
		_, err = CopyFile(filePath+dir2+"."+ext[1], PATH+v.Icon)
		// 搜集资源
		var fs FileSource
		err = db.Where("category_id = ?", v.ID).First(&fs).Error
		// 拆分归类资源
		ff := strings.Split(fs.CN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(cnPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
		ff = strings.Split(fs.EN, "#")
		for i1, f1 := range ff {
			_, err = CopyFile(enPath+"/"+strconv.Itoa(i1+100)+"@"+f1, PATH+f1)
			if err != nil {
				log.Fatalln(err)
			}
		}
	}
}

func CopyFile(dstName, srcName string) (written int64, err error) {
	src, err := os.Open(srcName)
	if err != nil {
		return
	}
	defer src.Close()
	dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		return
	}
	defer dst.Close()
	return io.Copy(dst, src)
}
